(function () {
    var Pipe = window.Pipe = function () {
        this.img1 = game.imgList.pipe_down;
        this.img2 = game.imgList.pipe_up;

        var allHeight = game.ctx.canvas.height * 0.78;
        //缝隙高度
        var interpace = 120;
        // console.log(this.img1.height);
        //上边管子随机显示的高度
        this.img1Height = Math.floor(Math.random()*240)+80;
        //下管子显示高度
        this.img2Height = allHeight - interpace - this.img1Height;

        this.x0 = game.ctx.canvas.width;
        this.y01 = 0;
        this.y02 = this.img1Height + interpace;
        this.y0 = game.ctx.canvas.height*0.78;
        //判断是否已经通过
        this.alreadyPass = false;
        //将创建的对象推入数组
        game.scene.pipeArr.push(this);
    };
    Pipe.prototype.update = function () {
        this.x0 -= 2;
        this.pipeB = this.img1Height;
        this.pipeT = this.y02;
        this.pipeL = this.x0;
        this.pipeR = this.x0 + this.img1.width;
        if((game.scene.bird.birdR>this.pipeL)&&(game.scene.bird.birdL<this.pipeR)){
            if((game.scene.bird.birdT<this.pipeB)||(game.scene.bird.birdB>this.pipeT)){
                game.scene.enter(4);
                game.scene.bird.isBirdDied = true;
                game.scene.isBirdPipe = true;
                // clearInterval(game.timer);
            }
        }

        if(game.scene.bird.birdR>this.pipeL && !this.alreadyPass && !game.scene.bird.isBirdDied){
            game.scene.score++;
            //标记已经通过了
            this.alreadyPass = true;
        }
        if(this.x0<-52){
            for(var i=0;i<game.scene.pipeArr.length;i++){
                if(this===game.scene.pipeArr[i]){
                    game.scene.pipeArr.splice(0,1);
                }
            }
        }
    };
    Pipe.prototype.render = function () {
        //绘制上面管子
        game.ctx.drawImage(this.img1, 0,this.img1.height - this.img1Height, this.img1.width,this.img1Height, this.x0,this.y01, this.img1.width,this.img1Height);
        //绘制下面管子
        game.ctx.drawImage(this.img2, 0,0, this.img2.width,this.img2Height, this.x0,this.y02, this.img2.width,this.img2Height);

        // game.ctx.fillStyle = "black";
        // game.ctx.fillText(this.x0,this.x0,100);
        // game.ctx.fillText(this.img1Height,this.x0+50,this.img1Height);
        // game.ctx.fillText(this.y02,this.x0+50,this.y02);
    };
}());